package com.thkj.modules.food.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.thkj.common.util.RequestUtil;
import com.thkj.modules.food.entity.TblFoodProp;
import com.thkj.modules.food.service.TblFoodPropService;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import dp.R;
import dp.dao.PagingList;
import dp.util.DateUtil;
import dp.web.BaseAction;

@RestController
@RequestMapping("/backstage/foodProp")
public class FoodPropAction extends BaseAction {
	@Autowired
	TblFoodPropService tblFoodPropService;

	@RequestMapping("")
	public ModelAndView index() {
		return new ModelAndView("foodProp/list");
	}

	@RequestMapping("/query")
	public R query(HttpServletRequest request, int page, int size) {
		Map<String, Object> param = RequestUtil.getParameterMap(request);
		PagingList<Map> list = tblFoodPropService.query(page, size, param);
		list.getList().forEach(t -> {
			t.put("outline", HtmlUtil.cleanHtmlTag(t.get("content").toString()).replaceAll("&nbsp;", ""));
		});
		return R.success(list);
	}

	@RequestMapping("/edit")
	public R edit(TblFoodProp foodProp) {
		try {
			if (StrUtil.isBlank(foodProp.getId())) {
				foodProp.setId(IdUtil.objectId());
				foodProp.setPublishTime(DateUtil.getNowAsTimestamp());
				tblFoodPropService.insertTblFoodProp(foodProp);
			} else {
				tblFoodPropService.updateTblFoodProp(foodProp);
			}
			return R.SUCCESS;
		} catch (Exception e) {
			return R.failure(e.getMessage());
		}
	}

	@RequestMapping("/delete")
	public R delete(String id) {
		tblFoodPropService.deleteTblFoodProp(id);
		return R.SUCCESS;
	}
}